/* 
 * @description:体重/血糖/血压/心率记录
 * @author: Jane 
 * @date: 2023-10-09 15:35:50
 */
import 'package:basic_chart/line_area_chart.dart';
import 'package:basic_chart/line_two_area_chart.dart';
import 'package:basic_common/app_theme.dart';
import 'package:basic_common/common_exports.dart';
import 'package:basic_widgets/button_widget.dart';
import 'package:basic_widgets/custom_sliver_appbar.dart';
import 'package:basic_widgets/image_widget.dart';
import 'package:basic_widgets/ruler/horizontal_numberpicker_wrapper.dart';
import 'info_record_controller.dart';

class InfoRecordPage extends GetView<InfoRecordController> {
  const InfoRecordPage({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SliverCustomAppBar(
        scrollController: controller.scrollController!,
        height: controller.kExpandedHeight,
        cover: 'assets/message/icon_patient.png',
        coverHeight: 240,
        radius: 12,
        bgChild: buildBackgroundWidget(),
        bgColor: controller.bgColor.value,
        title: Obx(() => Text(
              controller.isAppBArPinned.value
                  ? "${controller.typeName.value}检测"
                  : "",
              style:
                  TextStyle(color: Colors.white, fontSize: ScreenHelper.sp(16)),
            )),
        child: Column(children: [
          Container(
            alignment: Alignment.centerLeft,
            padding: EdgeInsets.fromLTRB(10, 10, 0, 0),
            child: Row(
              children: [
                Text(
                  "${controller.typeName.value}趋势",
                  style: TextStyle(
                      color: AppTheme.dartTextColor,
                      fontSize: ScreenHelper.sp(16)),
                ),
                SizedBox(
                  width: ScreenHelper.width(10),
                ),
                Text(
                  "(单位：${controller.unit.value})",
                  style: TextStyle(
                      color: AppTheme.weakTextColor,
                      fontSize: ScreenHelper.sp(13)),
                )
              ],
            ),
          )
        ]),
        slivers: [
          SliverList(
              delegate: SliverChildListDelegate([
            Stack(
              children: [
                Container(
                    margin: EdgeInsets.symmetric(horizontal: 10),
                    padding: EdgeInsets.all(6),
                    decoration: BoxDecoration(
                      color: Colors.white,
                    ),
                    height: ScreenHelper.height(145),
                    width: ScreenHelper.screenWidth,
                    child: controller.type == "press"
                        ? TwoLineAreaChart(
                            isShowingMainData: true,
                          )
                        : Container(
                            height: ScreenHelper.height(160),
                            child: LineAreaChart(
                              isShowingMainData: false,
                              lineColor: controller.bgColor.value,
                            ),
                          )
                    // child: Column(
                    //   children: [
                    //     SizedBox(
                    //       height: ScreenHelper.height(30),
                    //       width: ScreenHelper.screenWidth,
                    //       child: Row(
                    //         children: [_buildTabBar(),const Spacer()],
                    //       ),
                    //     ),
                    //     Expanded(
                    //       child: TabBarView(
                    //         controller: controller.tabController,

                    //         children: [
                    //           TwoLineAreaChart(
                    //             isShowingMainData: true,
                    //           ),
                    //           LineAreaChart(
                    //             isShowingMainData: false,
                    //             lineColor: controller.bgColor.value,
                    //           )
                    //         ],
                    //       ),
                    //     ),
                    //   ],
                    // ),
                    ),
                Positioned(
                    left: ScreenHelper.width(30),
                    top: ScreenHelper.width(5),
                    child: Opacity(
                      opacity: controller.type == "press" ? 1 : 0,
                      child: Row(
                        children: [
                          Text(
                            "收缩压",
                            style: TextStyle(
                                color: AppTheme.dartWeakTextColor,
                                fontSize: ScreenHelper.sp(13)),
                          ),
                          SizedBox(
                            width: ScreenHelper.width(10),
                          ),
                          svgImage(
                              assets: "assets/common/icon_legend.svg",
                              colorFilter: const ColorFilter.mode(
                                  Color.fromRGBO(45, 207, 152, .5),
                                  BlendMode.srcIn),
                              package: "basic_common"),
                          SizedBox(
                            width: ScreenHelper.width(10),
                          ),
                          Text(
                            "舒张压",
                            style: TextStyle(
                                color: AppTheme.dartWeakTextColor,
                                fontSize: ScreenHelper.sp(13)),
                          ),
                          SizedBox(
                            width: ScreenHelper.width(10),
                          ),
                          svgImage(
                              assets: "assets/common/icon_legend.svg",
                              colorFilter: const ColorFilter.mode(
                                  Color.fromRGBO(253, 218, 121, .5),
                                  BlendMode.srcIn),
                              package: "basic_common"),
                        ],
                      ),
                    )),
              ],
            )
          ])),
          SliverList(
              delegate: SliverChildListDelegate([
            Stack(
              children: [
                Padding(
                  padding: EdgeInsets.only(top: 10, left: 15),
                  child: Text(
                    "${controller.typeName.value}记录",
                    style: TextStyle(
                        color: AppTheme.dartTextColor,
                        fontSize: ScreenHelper.sp(16)),
                  ),
                ),
              ],
            )
          ])),
          SliverFixedExtentList(
            delegate: SliverChildBuilderDelegate(
                (context, index) => _buildListItem(),
                childCount: 15),
            itemExtent: 100.0,
          ),
        ],
      ),
    );
  }

// 背景隐藏内容
  Widget buildBackgroundWidget() {
    return Container(
      color: controller.bgColor.value,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          SizedBox(height: ScreenHelper.topBarHeight - 38),
          Text(
            "2021年12月30日 13:00",
            style: TextStyle(color: Colors.white.withOpacity(.8)),
          ),
          SizedBox(height: ScreenHelper.height(10)),
          Expanded(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Obx(() => Text(
                      "${controller.weightValue.value}",
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: ScreenHelper.sp(40),
                          fontWeight: FontWeight.bold),
                    )),
                Text(
                  "${controller.unit.value}",
                  style: TextStyle(
                      color: Colors.white, fontSize: ScreenHelper.sp(13)),
                )
              ],
            ),
          ),
          SizedBox(
            height: ScreenHelper.height(5),
          ),
          Text(
            controller.type == "weight" ? "BMI 20.7 标准" : "",
            style: TextStyle(
                color: Colors.white.withOpacity(.8),
                fontSize: ScreenHelper.sp(14)),
          ),
          SizedBox(
            height: ScreenHelper.height(15),
          ),
          GestureDetector(
            onTap: () {
              showHorNum();
            },
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 6, vertical: 4),
              decoration: BoxDecoration(
                  color: Colors.yellow,
                  borderRadius: BorderRadius.circular(50)),
              child: Text(
                "记录${controller.typeName.value}",
                style: TextStyle(
                    color: Colors.white, fontSize: ScreenHelper.sp(14)),
              ),
            ),
          ),
          SizedBox(
            height: ScreenHelper.height(25),
          ),
        ],
      ),
    );
  }

  // 记录体重ListItem
  Widget _buildListItem() {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 8),
      margin: EdgeInsets.all(ScreenHelper.width(10)),
      decoration: const BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(Radius.circular(10)),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text(
                "1/24",
                style: TextStyle(
                    color: AppTheme.dartWeakTextColor,
                    fontSize: ScreenHelper.sp(14)),
              ),
              Text(
                "20:20",
                style: TextStyle(
                    color: AppTheme.weakTextColor,
                    fontSize: ScreenHelper.sp(13)),
              ),
            ],
          ),
          Container(
            width: 1,
            color: Colors.blue.withOpacity(.2),
            height: ScreenHelper.height(90),
          ),
          Row(
            children: [
              Text.rich(
                TextSpan(
                  text: '52.0',
                  style: TextStyle(
                      color: AppTheme.dartTextColor,
                      fontSize: ScreenHelper.sp(28)),
                  children: <InlineSpan>[
                    TextSpan(
                        text: '${controller.unit.value}',
                        style: TextStyle(
                            color: AppTheme.weakTextColor,
                            fontSize: ScreenHelper.sp(14))),
                  ],
                ),
              ),
              SizedBox(
                width: ScreenHelper.width(15),
              ),
              Container(
                padding: EdgeInsets.symmetric(
                    horizontal: ScreenHelper.width(8),
                    vertical: ScreenHelper.width(6)),
                decoration: BoxDecoration(
                    color: AppTheme.primyColor.withOpacity(.3),
                    borderRadius: BorderRadius.circular(10)),
                child: Text(
                  "正常",
                  style: TextStyle(
                      color: AppTheme.primyColor,
                      fontSize: ScreenHelper.sp(14)),
                ),
              ),
              SizedBox(
                width: ScreenHelper.width(15),
              ),
              IconButton(
                onPressed: () {
                  showHorNum();
                },
                icon: Image.asset("assets/patient/icon_update.png",
                    package: "basic_common"),
              ),
              IconButton(
                onPressed: () {
                  showDelDialog();
                },
                icon: Image.asset("assets/patient/icon_del.png",
                    package: "basic_common"),
              )
            ],
          )
        ],
      ),
    );
  }

  // 底部 dialog
  void showHorNum() {
    Get.bottomSheet(Container(
      color: Get.isDarkMode ? Colors.black12 : Colors.white,
      height: ScreenHelper.height(280),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Text("${controller.typeName.value}记录",
                textAlign: TextAlign.center,
                style: TextStyle(
                    color:
                        Get.isDarkMode ? Colors.white : AppTheme.dartTextColor,
                    fontSize: ScreenHelper.sp(14))),
          ),
          SizedBox(
            height: ScreenHelper.width(15),
          ),
          _buildRulerWidget(),
          SizedBox(
            height: ScreenHelper.height(20),
          ),
          SizedBox(
            // height: ScreenHelper.height(140),
            child: Row(
              children: [
                SizedBox(
                  width: ScreenHelper.width(15),
                ),
                Expanded(
                    child: connerButton(
                        onPressed: () {},
                        textStr: "取消",
                        bgColor: Colors.white,
                        textColor: AppTheme.bluetextColor,
                        borderColor: AppTheme.dividerColor)),
                SizedBox(
                  width: ScreenHelper.width(20),
                ),
                Expanded(
                    child: connerButton(
                        onPressed: () {
                          Get.back();
                        },
                        textStr: "确定",
                        bgColor: AppTheme.bluetextColor,
                        textColor: Colors.white,
                        borderColor: AppTheme.bluetextColor)),
                SizedBox(
                  width: ScreenHelper.width(15),
                ),
              ],
            ),
          ),
          SizedBox(
            width: ScreenHelper.width(20),
          ),
        ],
      ),
    ));
  }

// 标尺
  _buildRulerWidget() {
    return HorizontalNumberPickerWrapper(
      initialValue: controller.weightValue.value.toInt() * 10,
      minValue: controller.type != "sugar" ? 100 : 10,
      maxValue: 1500,
      step: 1,
      unit: '${controller.unit.value}',
      widgetWidth: MediaQuery.of(Get.context!).size.width.round() - 30,
      subGridCountPerGrid: 10,
      subGridWidth: 8,
      titleTextColor: AppTheme.dartTextColor,
      onSelectedChanged: (value) {
        controller.weightValue.value = (value / 10);
      },
      titleTransformer: (value) {
        return formatIntegerStr(value / 10);
      },
      scaleTransformer: (value) {
        return formatIntegerStr(value / 10);
      },
    );
  }

// 删除dialog
  void showDelDialog() {
    showDialog(
      context: Get.context!,
      builder: (context) => UnconstrainedBox(
        // 只有这个方向的约束生效
        constrainedAxis: Axis.horizontal,
        child: Container(
          height: ScreenHelper.height(140),
          margin: EdgeInsets.symmetric(horizontal: ScreenHelper.width(60)),
          padding: const EdgeInsets.symmetric(vertical: 20),
          decoration: BoxDecoration(
              color: Colors.white, borderRadius: BorderRadius.circular(10)),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Center(
                child: Text(
                  "确定删除该条${controller.typeName.value}记录吗？",
                  style: TextStyle(
                      color: AppTheme.dartWeakTextColor,
                      fontSize: ScreenHelper.sp(18)),
                ),
              ),
              Padding(
                padding: EdgeInsets.symmetric(horizontal: 20),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: [
                    GestureDetector(
                      onTap: () => Get.back(),
                      child: Text(
                        "取消",
                        style: TextStyle(
                            color: AppTheme.dartWeakTextColor,
                            fontSize: ScreenHelper.sp(16)),
                      ),
                    ),
                    GestureDetector(
                      onTap: () => Get.back(),
                      child: Text(
                        "确定",
                        style: TextStyle(
                            color: AppTheme.bluetextColor,
                            fontSize: ScreenHelper.sp(16)),
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  // Tab切换
  Widget _buildTabBar() {
    return DefaultTabController(
      length: 2,
      child: Container(
        height: ScreenHelper.height(50),
        width: ScreenHelper.width(120),
        padding: EdgeInsets.symmetric(vertical: ScreenHelper.width(4)),
        decoration: BoxDecoration(
            color: Colors.grey[300]!.withOpacity(.5),
            borderRadius: BorderRadius.circular(10)),
        child: TabBar(
          controller: controller.tabController,
          labelColor: AppTheme.bluetextColor,
          labelPadding: EdgeInsets.symmetric(horizontal: 4, vertical: 2),
          labelStyle: TextStyle(fontSize: ScreenHelper.sp(13)),
          unselectedLabelColor: AppTheme.dartWeakTextColor,
          indicatorSize: TabBarIndicatorSize.label,
          indicator: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(
              Radius.circular(10),
            ),
          ),
          tabs: [
            Tab(
              child: Align(
                alignment: Alignment.center,
                child: Text("餐前"),
              ),
            ),
            Tab(
              child: Align(
                alignment: Alignment.center,
                child: Text("餐后"),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

///去掉整数后的小数点和0
///1.0 -> "1"
///1.2 -> "1.2"
String formatIntegerStr(num number) {
  int intNumber = number.truncate();

  //是整数
  if (intNumber == number) {
    return intNumber.toString();
  } else {
    return number.toString();
  }
}

class CustomFloatingActionButtonLocation extends FloatingActionButtonLocation {
  FloatingActionButtonLocation location;
  double offsetX; // X方向的偏移量
  double offsetY; // Y方向的偏移量
  CustomFloatingActionButtonLocation(this.location, this.offsetX, this.offsetY);

  @override
  Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
    Offset offset = location.getOffset(scaffoldGeometry);
    return Offset(offset.dx + offsetX, offset.dy + offsetY);
  }
}
